package com.yml.mall.category;

import com.yml.mall.category.dao.CategoryMapper;
import com.yml.mall.category.pojo.CategoryDO;
import com.yml.mall.category.pojo.CategoryInsertDTO;
import com.yml.mall.category.pojo.CategoryStructureDTO;
import com.yml.mall.currentexception.MyException;
import com.yml.mall.enums.ResponseEnum;
import com.yml.mall.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CategoryService
 * @Author Administrator
 * @Date 2020/4/8   16:37
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    private final CategoryMapper categoryMapper;
    private CategoryServiceImpl(CategoryMapper categoryMapper){
        this.categoryMapper=categoryMapper;
    }
    /**
     * 获取整个类目结构 不推荐这种做法需要多次链接数据库(有根目录的大小和目录深度决定)
     * @param root
     * @
     * @return
     */
    @Override
    public void getCategoryStructure(List<CategoryStructureDTO> root,List<CategoryStructureDTO> current){
        if(root == null) root=new ArrayList<CategoryStructureDTO>();
        if (current!=null && current.size()>0){
            //复制上层类目的结构
            for(int i=0;i<current.size();i++){

                root.add(current.get(i));
                current.get(i).setSubCategoryStructureDTOs(new ArrayList<>());
                //查找子目录
                getCategoryStructure(current.get(i).getSubCategoryStructureDTOs(),categoryVOs2CategoryStructureDTOs(categoryMapper.selectByParentId(current.get(i).getId())));
            }
        }
    }


    @Override
    public ResponseVO<List<CategoryStructureDTO>>  CategoryStructureDTO(){
        List<CategoryDO> categoryDOS=categoryMapper.selectAll();
        List<CategoryStructureDTO> categoryStructureDTOS=categoryVOs2CategoryStructureDTOs(categoryDOS);
        List<CategoryStructureDTO> current=new ArrayList<>();
        //System.out.println(categoryStructureDTOS.toString());
        for(int i=0;i<categoryStructureDTOS.size();i++){
            if(categoryStructureDTOS.get(i).getParentId()!=null && categoryStructureDTOS.get(i).getParentId()==0){
                current.add(categoryStructureDTOS.get(i));
                findAllSubItem(categoryStructureDTOS.get(i),categoryStructureDTOS);
            }
        }
        return ResponseVO.success(current);
    }


    @Override
    public ResponseVO<List<CategoryStructureDTO>> save(CategoryInsertDTO categoryInsertDTO) {

        if(categoryInsertDTO.getName().equals(null))
            throw new MyException(ResponseEnum.FIELD_ERROR);
        //不存在父目录
        if(categoryInsertDTO.getParentId()!=null && !categoryInsertDTO.getParentId().equals(0)){
            CategoryDO categoryDO=categoryMapper.selectByPrimaryKey(categoryInsertDTO.getParentId());
            if(categoryDO==null)
                throw new MyException(ResponseEnum.FIELD_ERROR);
        }
        CategoryDO categoryDO=new CategoryDO();
        BeanUtils.copyProperties(categoryInsertDTO,categoryDO);

        categoryMapper.insertSelective(categoryDO);
        return ResponseVO.success();
    }

    public void save(CategoryStructureDTO categoryStructureDTO) {
        if (categoryStructureDTO!=null){
            CategoryDO categoryDO =new CategoryDO();
            log.info("1");
            BeanUtils.copyProperties(categoryDO,categoryStructureDTO);
            categoryMapper.insertSelective(categoryDO);
            for(int i=0;i<categoryStructureDTO.getSubCategoryStructureDTOs().size();i++)
                save(categoryStructureDTO.getSubCategoryStructureDTOs().get(i));
        }
    }


    //parentid=0为根目录
    @Override
    public ResponseVO<List<CategoryStructureDTO>> getAllRootItem() {
        List<CategoryDO> categoryDOS=categoryMapper.selectByParentId(0);
        List<CategoryStructureDTO> categoryStructureDTOS=categoryVOs2CategoryStructureDTOs(categoryDOS);
        return ResponseVO.success(categoryStructureDTOS);
    }


    @Override
    public ResponseVO<List<CategoryStructureDTO>> getSubItem(Integer parentId) {
        if(parentId==null || parentId<0){
            throw new MyException(ResponseEnum.FIELD_ERROR);
        }
        List<CategoryDO> categoryDOS=categoryMapper.selectByParentId(parentId);
        return ResponseVO.success(categoryVOs2CategoryStructureDTOs(categoryDOS));
    }


    @Override
    public ResponseVO<List<CategoryStructureDTO>> addRootItem( CategoryStructureDTO categoryStructureDTO) {
        if(categoryStructureDTO ==null ){
            throw new MyException(ResponseEnum.OBJECT_ERROR);
        }
        if(categoryStructureDTO.getParentId()==null){
            throw new MyException(ResponseEnum.FIELD_ERROR);
        }
        CategoryDO categoryDO=new CategoryDO();
        BeanUtils.copyProperties(categoryStructureDTO,categoryDO);

        int i=categoryMapper.insert(categoryDO);
        if(i==0)return ResponseVO.error(ResponseEnum.ERROR);
        return ResponseVO.success();
    }

    //List<CategoryDO> 转换成 List<CategoryStructureDTO>
    private List<CategoryStructureDTO> categoryVOs2CategoryStructureDTOs(List<CategoryDO> categoryDOS){
        List<CategoryStructureDTO> categoryStructureDTOS=new ArrayList<>();
        for(int i=0;i<categoryDOS.size();i++){
            CategoryStructureDTO categoryStructureDTO=new CategoryStructureDTO();
            BeanUtils.copyProperties(categoryDOS.get(i),categoryStructureDTO);
            categoryStructureDTOS.add(categoryStructureDTO);
        }
        return categoryStructureDTOS;
    }
    //获取该目录的所有子目录
    private  void findAllSubItem(CategoryStructureDTO categoryStructureDTO,List<CategoryStructureDTO> categoryStructureDTOS){
        if(categoryStructureDTO!=null){
            //这条语句被忘了
            categoryStructureDTO.setSubCategoryStructureDTOs(new ArrayList<>());
            for(int i=0;i<categoryStructureDTOS.size();i++){
                //System.out.println(categoryStructureDTO.getId()+" "+categoryStructureDTOS.toString());
                //Integer 判断用 equals
                if(categoryStructureDTOS.get(i).getParentId().equals(categoryStructureDTO.getId())){
                    categoryStructureDTO.getSubCategoryStructureDTOs().add(categoryStructureDTOS.get(i));
                   // System.out.println("hello"+categoryStructureDTO.getSubCategoryStructureDTOs().toString());
                    findAllSubItem(categoryStructureDTOS.get(i),categoryStructureDTOS);
                }
            }
            if(categoryStructureDTO.getSubCategoryStructureDTOs().size()<=0) categoryStructureDTO.setSubCategoryStructureDTOs(null);
        }
    }

}
